package com.lzm.back.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzm.back.entity.Registration;
import com.lzm.back.entity.Score;
import com.lzm.back.entity.Status;
import com.lzm.back.mapper.RegistrationMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RegistrationService {
    @Resource
    private RegistrationMapper service;
    @Resource
    private ScoreService scoreService;

    public PageInfo<Registration> selectPage(Registration registration, String username, String name, Status status, LocalDateTime applyTime, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<Registration> eventList = service.selectByCondition(username, name, status, registration.getUserId());

        return PageInfo.of(eventList);
    }
    public void addRegistrationInformation(Registration registration) {
        registration.setStatus(Status.PENDING);
        service.insert(registration);
    }

    public void updateRegistrationInformation(Registration registration) {
        service.updateByPrimaryKey(registration);
        if (registration.getStatus() == Status.APPROVED) {
            //在分数表中新增数据
            Score score = new Score();
            score.setUserId(registration.getUserId());
            score.setEventId(registration.getEventId());
            scoreService.addScore(score);
        }
    }

    public void delRegistrationInformation(Long id) {
        service.deleteByPrimaryKey(id);
    }

    public void delRegistrationInformationBaTch(List<Long> ids) {
        for (Long id : ids) {
            service.deleteByPrimaryKey(id);
        }
    }
}
